<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\BoxCollocation;
use App\Models\Box;
use App\Models\Colour;
use App\Models\BoxRetreat;
use App\Models\BoxAddress;
use App\Models\UsersWechat;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;

/**
 * 换货处理
 */
class BoxChangeController extends Controller
{
    public function __construct()
    {
        $this->middleware('admin');
    }

    public function change(Request $request){
        $data['colours'] = Colour::get();
        return view('admin/box/change',$data);
    }

    public function getInfo($id){
        $row = BoxRetreat::find($id);
        if (!$row) {
            return $this->error('记录不存在');
        }
        return $this->response(['row'=>$row]);
    }

    public function getPageRows(Request $request){
        $box_id = intval($request->input('box_id'));
        $type = intval($request->input('type',1));
        $status = intval($request->input('status'));
        $query = BoxRetreat::with('box','user')->orderBy('id','DESC');
        if ($box_id) {
            $query->where('box_id',$box_id);
        }
        if ($status) {
            $query->where('status',$status);
        }
        $rows = $query->where('type',$type)->paginate(20);
        foreach ($rows as $key => $row) {
            //换获
            if ($row->type==1) {
                switch ($row->status) {
                    case 0:
                        $row->statusName = '拒绝申请';
                        break;
                    case 1:
                        $row->statusName = '等待客户填写物流快递信息';
                        break;
                    case 2:
                        $row->statusName = '待发货';
                        break;
                    case 3:
                        $row->statusName = '已发货';
                        break;
                    case 4:
                        $row->statusName = '完成';
                        break;
                    default:
                        $row->statusName = '异常状态';
                        break;
                }
                $row->address = BoxAddress::where('box_id',$row->box_id)->where('type','box')->first();
                $row->collocation = BoxCollocation::select('style_number','size','colour')->find($row->collocation_id);
            }

        }
        $data =['rows'=>$rows];
        return $this->response($data);
    }

    public function getPhotos($id){
        $row = BoxRetreat::find($id);
        if (!$row) {
            return $this->error('记录不存在');
        }
        $array = [];
        $array['data'] = [];
        foreach ($row->pic as $key => $value) {
            array_push($array['data'],['src'=>$value]);
        }
        return $this->response($array);
    }
    public function postChange(Request $request){
        $id = intval($request->input('id'));
        $status = intval($request->input('status'));
        $row = BoxRetreat::find($id);
        if (!$row) {
            return $this->error('记录不存在');
        }
        if ($row->type==1) {
            if ($row->status==5) {
                 $row->status=6;//完成
            }else{
                 $row->status=$status;
            }
        }else{
             $row->status=$status;
        }

        $row->save();
        return $this->response('ok');
    }

    // public function getCollectSend($id){
    //     $retreat = BoxRetreat::find($id);
    //     if (!$retreat) {
    //         return $this->error('记录不存在');
    //     }
    //     if ($retreat->status>=5) {
    //         return $this->error('该申请已经处理');
    //     }
    //     $retreat->status = 3;
    //     $retreat->save();

    //     $name = safe2($request->input('name'));
    //     $no = safe2($request->input('no'));
    //     $estimate_at = safe2($request->input('estimate_at'));

    //     $row = new BoxDeliver;
    //     $row->box_id = $box->id;
    //     $row->name = $name;
    //     $row->no = $no;
    //     $row->estimate_at = $estimate_at;
    //     $row->delivery_at = date('Y-m-d H:i:s');
    //     $row->type='retreat';
    //     $row->Save();

    //     //删除原来的配置
    //     // BoxCollocation::where('id',$retreat->collocation_id)->delete();


    //     //重新配置
    //     // $title = safe2($request->input('title'));
    //     // $style_number = safe2($request->input('style_number'));
    //     // $price = intval($request->input('price'));
    //     // $p_number = intval($request->input('p_number',1));
    //     // $pic = safe2($request->input('pic'));
    //     // $size = safe2($request->input('size'));
    //     // $colour = safe2($request->input('colour'));
    //     // $id = intval($request->input('id'));

    //     // $collocation = new BoxCollocation;
    //     // $collocation->box_id = $retreat->box_id;
    //     // $collocation->wechat_id= $retreat->wechat_id;
    //     // $collocation->pic = $pic;
    //     // $collocation->colour =  $colour;
    //     // $collocation->style_number=$style_number;
    //     // $collocation->size=$size;
    //     // $collocation->title=$title;
    //     // $collocation->price=$price;
    //     // $collocation->p_number=$p_number;
    //     // $collocation->save();
    //     return $this->response('ok');
    // }

    public function postSend(Request $request){
        $id = intval($request->input('id'));
        $row = BoxRetreat::find($id);
        if(empty($row)){
            return $this->error('记录不存在');
        }
        // if ($row->type!=1) {
        //     return $this->error('退货信息无需发货');
        // }
        // if ($row->status!=2) {
        //     return $this->error('状态错误');
        // }
        $name = safe2($request->input('name'));
        $no = safe2($request->input('no'));
        $estimate_at = safe2($request->input('estimate_at'));

        $row->to_send=  serialize(['no'=>$no,'name'=>$name,'created_at'=> date('Y-m-d H:i:s'),'estimate_at'=>$estimate_at]);
        $row->status=3;
        $row->Save();
        return $this->response('ok');
    }
}
